﻿namespace SanlieHash.OtherHash
{
    using System;
    public class BCC : IHash
    {
        public int HashBits => 16;
        internal static BCC_CTX Init()
        {
            var ctx = new BCC_CTX();
            ctx.check = 0;
            return ctx;
        }
        internal static void Update(ref BCC_CTX ctx, byte[] data, int length)
        {
            for (uint i = 0; i < length; i++) ctx.check ^= data[i];
        }
        internal static void Update(ref BCC_CTX ctx, byte[] data, int start, int length)
        {
            for (uint i = 0; i < length; i++) ctx.check ^= data[i+ start];
        }
        internal static byte[] Final(ref BCC_CTX ctx)
        {
            var md = new byte[1];
            md[0] = ctx.check;
            return md;
        }
        public string Make(byte[] data)
        {
            return Helper.ToHexString(MakeBytes(data));
        }
        public byte[] MakeBytes(byte[] data)
        {
            var ctx = Init();
            Update(ref ctx, data, data.Length);
            return Final(ref ctx);
        }
        internal BCC_CTX _ctx;
        public void New()
        {
            _ctx = Init();
        }
        public void Update(byte[] data)
        {
            Update(ref _ctx, data, data.Length);
        }
        public void Update(byte[] data, int length)
        {
            Update(ref _ctx, data, length);
        }
        public string Make()
        {
            return Helper.ToHexString(Final(ref _ctx));
        }
        public byte MakeByte()
        {
            return _ctx.check;
        }

        public void Update(byte[] data, int start, int length)
        {
            Update(ref _ctx, data, start, length);
        }

        public byte[] MakeBytes()
        {
            return Final(ref _ctx);
        }
    }
}
